<?php
namespace app\home\home;
use think\Controller;
class Base extends controller
{

    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: GET,POST");
        header('Access-Control-Allow-Headers: X-Requested-With,content-type,if-modified-since,Auth');
        header('Content-Type: text/html; charset=UTF-8');
    }


    /**
     * @func 验证token是否有效
     * @param $id当前登录的用户id
     */
    protected function checktoken($id){
        if(!isset($_SERVER['HTTP_AUTH'])){
            apistatus(2,'请输入你的token');
        }
        $token = $_SERVER['HTTP_AUTH'];
        if(empty($token)){
            apistatus(2,'请输入token');
        }else{
            $userarr=db("home_users")->where(['id'=>$id])->find();
            if($userarr){
                if($userarr['usertoken'] != $token){
                    apistatus(2,'你的token错误，请从新登录');
                }elseif($userarr['usertoken_expire'] < time()){
                    apistatus(2,'你的token已经过期请重新登录');
                }
            }else{
                apistatus(0,'没有找到用户');
            }
        }
    }



}
